<?php namespace App\Http\Services;

use App\ActiveParty;
use App\Activity;
use App\ActivityBrowseRecord;
use App\ActivityJoin;
use App\Admin;
use App\Classify;
use App\Comment;
use App\CommentLike;
use App\DemandButt;
use App\Demands;
use App\ExpertLabel;
use App\ExpertQualificationImg;
use App\Experts;
use App\Files;
use App\Images;
use App\InvoiceRecord;
use App\Message;
use App\MessageRemind;
use App\PersonalAuth;
use App\UnitAuth;
use App\UnitBindVipPackage;
use App\UnitBusinessLabel;
use App\UnitGrade;
use App\Units;
use App\User;
use App\UserAttentionActivity;
use App\UserAttentionDemand;
use App\UserAttentionNews;
use App\UserInfo;
use App\UserXUnit;
use App\VipInfo;
use App\VipPackage;
use App\VipPackageXRule;
use App\VipRule;
use Carbon\Carbon;
use Auth;
use phpDocumentor\Reflection\Types\Self_;

class CommonManager
{

    /**
     * 获取用户的基本信息
     * @param $user_id
     * @return array|null
     */
    public static function getUserBasicInfo($user_id){
        if ($user_id==0 || $user_id=='' || $user_id==null)
            return [];

        $userInfo   =UserInfo::whereUserId($user_id)->first();
        $user       =User::whereId($user_id)->first();
        $result=[];
        if ($userInfo){
            $result['user_id']  =$userInfo->user_id;
            $result['nickname'] =$userInfo->nickname;
            $result['realname'] =$userInfo->realname;
            $result['sex']      =$userInfo->sex==0 ? '保密' : $userInfo->sex==1 ? '男' : '女';
            $result['photo_url']=FileManager::getImgUrl($userInfo->photo_id);
            if (!is_null($userInfo->province))
                $result['province']=$userInfo->province;
            else
                $result['province']='';

            if (!is_null($userInfo->city))
                $result['city']=$userInfo->city;
            else
                $result['city']='';

            if (!is_null($userInfo->area))
                $result['area']=$userInfo->area;
            else
                $result['area']='';

            if (!is_null($userInfo->address))
                $result['address']=$userInfo->address;
            else
                $result['address']='';

            //手机号
            $result['phone']       = $user ? $user->phone : '用户不存在';
        }

        return $result;
    }

    /**
     * 手机号格式验证
     * @param string $phone
     * @return bool
     */
    public static function phoneVerification($phone = ''){
        if(empty($phone))
            return false;

        if(!preg_match("/^(1[34578])[0-9]{9}$/", trim($phone)))
            return false;

        return true;
    }

    /**
     * 信用代码正则
     * @param string $uniformSocialCreditCode
     * @return bool
     */
    public static function uniformSocialCreditCodeVerification($uniformSocialCreditCode = ''){
        if(empty($uniformSocialCreditCode))
            return false;

        if(!preg_match("/[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/", trim($uniformSocialCreditCode)))
            return false;

        return true;
    }

    /**
     * 身份证号格式验证
     * @param string $idCard
     * @return bool
     */
    public static function idCardVerification($idCard = ''){
        if(empty($idCard))
            return false;

        if(!preg_match("/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/", trim($idCard)))
            return false;

        return true;
    }

    /**
     * 根据营业执照图片获取相应信息
     * @param int $imgId
     * @return bool|object
     */
    public static function getUnitInfoToImg($imgId = 0){
        if(empty($imgId))
            return false;

        /** @var Images $thisImage */
        $thisImage = Images::whereId($imgId)->first();
        if(!$thisImage)
            return false;

        /** @var Files $thisFile */
        $thisFile = Files::whereId($thisImage->file_id)->first();
        if(!$thisFile)
            return false;

        $FileBase64 = ToolsManager::base64EncodeImage($thisFile->getLocalPath());

        $carBrandDiscern = new BusinessLicense();
        $result = $carBrandDiscern->businessLicenseIdentify($FileBase64);

        if(empty($result))
            return false;

        if(!empty($result->error_code))
            return false;


        if(empty($result->words_result))
            return false;

        $data = (object)[
            'a' => $result->words_result->单位名称->words,
            'b' => $result->words_result->社会信用代码->words,
            'c' => $result->words_result->法人->words,
            'd' => $result->words_result->注册资本->words,
            'e' => $result->words_result->成立日期->words,
            'f' => $result->words_result->地址->words,
            'g' => $result->words_result->经营范围->words,
            'h' => $result->words_result->类型->words,
            'i' => $result->words_result->有效期->words,
        ];

        return $data;
    }

}